﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using System.Linq.Dynamic;
using Yipusoft.Common.BLL;
using Yipusoft.VehicleReport.BLL;
using Yipusoft.VehicleReport.Model;

public partial class Police_SecurityEvaluate_AnlyseVehicleDay : System.Web.UI.Page
{

    int popedom = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //权限

            if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400201"))//总队
            {
                popedom = 0;
            }
            else if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400202"))//支队
            {
                Response.Redirect("SquadronAnlyseVehicleDay.aspx");
            }
            else if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400203"))//中队
            {
                Response.Redirect("List.aspx");

            }
            else
            {
                Response.Redirect("List.aspx");
                return;
            }
            BindDateList();
            BindDate();
        }
    }


    private void BindDate()
    {


        int year = Convert.ToInt32(DptYear.SelectedValue);
        int month = Convert.ToInt32(DptMonth.SelectedValue);
        int monthDayCount = Account.GetMonthDayCount(year, month);

        List<ChartItem> list = new List<ChartItem>();
        List<DepartmentInfo> departments = Department.GetList("departmentid like '%000' and departmentid<>'1300000' ");
        foreach (DepartmentInfo info in departments)
        {
            //中队个数
            int departmentCount = Department.GetList("departmentid like '" + info.DepartmentID.Substring(0, 4) + "3%'").Count;
            int totalCount = departmentCount * monthDayCount;
            string where = " departmentId like '" + info.DepartmentID.Substring(0, 4) + "%' and year(Daydate)=" + year + " and month(daydate)=" + month + " ";
            int RealCount = VehicleDayItem.GetVichileDayCount(where);
            double percent = (double)(totalCount - RealCount) / totalCount;
            list.Add(new ChartItem()
            {
                DepartmentId = info.DepartmentID,
                DepartmentName = info.AliasName,
                Percent = percent
            });

        }
        list = list.OrderByDescending(o => o.Percent).ToList();
        UserChart.Series["车辆装备检查"].Points.DataBindXY(list, "DepartmentName", list, "Percent");
        UserChart.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        UserChart.Series["车辆装备检查"].LabelFormat = "0.00" + "%";
        for (int i = 0; i < list.Count; i++)
        {
            UserChart.Series["车辆装备检查"].Points[i].Url = "SquadronAnlyseVehicleDay.aspx?Departmentid=" + Convert.ToString(list[i].DepartmentId) + "&y=" + DptYear.SelectedValue + "&m=" + DptMonth.SelectedValue;
        }
    }


    private void BindDateList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            DptYear.Items.Add(li);
        }
        for (int i = 1; i <= 12; i++)
        {
            this.DptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        if (Request.QueryString["year"] == null)
        {
            DptMonth.SelectedValue = DateTime.Now.Month.ToString();
            DptYear.SelectedValue = DateTime.Now.Year.ToString();
        }
        else
        {
            DptMonth.SelectedValue = Request["month"];
            DptYear.SelectedValue = Request["year"];
        }
    }

    protected void DptYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void DptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }


    [Serializable]
    public class ChartItem
    {
        public string DepartmentId
        {
            set;
            get;
        }

        public string DepartmentName
        {
            set;
            get;
        }
        public double Percent
        {
            set;
            get;
        }
    }
}